using System;
using System.Collections.Generic;
using System.Text;

namespace v6502
{
    class Assmebler
    {
        private RAM memory;
        private int currentLocation = 0x600;
        private InstructionSet instructionSet;

        public Assmebler(InstructionSet ins, RAM ram)
        {
            memory = ram;
            instructionSet = ins;
        }
        public void Assemble(string tmp)
        {
            //load(instructionSet.opcode("BNE $12"));
            //instructionSet.opcode(tmp);
            /*
            // a5fe 8500 a5fe 2903 1869 0285 01a5 fea0 0091 00
            // 19 bytes
            load(instructionSet.ASMtoMC("lda $fe"));
            load(instructionSet.ASMtoMC("sta $00"));
            load(instructionSet.ASMtoMC("lda $fe"));
            load(instructionSet.ASMtoMC("and #$3"));
            load(instructionSet.ASMtoMC("clc"));
            load(instructionSet.ASMtoMC("adc #$2"));
            load(instructionSet.ASMtoMC("sta $01"));
            load(instructionSet.ASMtoMC("lda $fe"));
            load(instructionSet.ASMtoMC("ldy #$0"));
            load(instructionSet.ASMtoMC("sta ($00),y"));
            */

            // a200 8600 e600 a400 c062 a000 8400 a900 9d00 04a9 019d 0004
            // 24 bytes
            /*load(instructionSet.ASMtoMC("ldx #0"));
            load(instructionSet.ASMtoMC("stx $0"));
            load(instructionSet.ASMtoMC("inc $0"));
            load(instructionSet.ASMtoMC("ldy $0"));
            load(instructionSet.ASMtoMC("cpy #62"));
            load(instructionSet.ASMtoMC("ldy #$0"));
            load(instructionSet.ASMtoMC("sty $0"));
            load(instructionSet.ASMtoMC("lda #$0"));
            load(instructionSet.ASMtoMC("sta $400,x"));
            load(instructionSet.ASMtoMC("lda #1"));
            load(instructionSet.ASMtoMC("sta $400,x"));*/

            //load(instructionSet.ASMtoMC("jmp loop\nloop: lda $fe"));
            //load(instructionSet.ASMtoMC("loop:\nlda $fe\nsta $00\nlda $fe\njmp test\nand #$3\nclc\nadc #$2\ntest:\nsta $01\nlda $fe\nldy #$0\nsta ($00),y\njmp loop"));
            
            // test string below
            //load(instructionSet.ASMtoMC("start: ldy #$ff\nldx #$0\nloop: lda $fe\nsta $200,x\nand #$7\nsta $300,x\nand #$3\nsta $400,x\nand #$1\nsta $500,x\ninx\ndey\nbne loop\nrts"));
            
            //string ts = "loop: lda $fe";
            
            //instructionSet.test(ts);

            //load(instructionSet.ASMtoMC("jmp loop"));

            load(instructionSet.ASMtoMC("lda #$77\nsta $200\ndec $200\nlda $200"));
        }

        private void load(byte[] data)
        {
            for(int i = 0; i < data.Length; i++)
            {
                memory.write(currentLocation, data[i]);
                currentLocation++;
            }
        }
    }
}
